<?php
session_start();
require_once("../config/include_tokyo.inc.php");
require_once("../class/Common_Function.php");
require_once("../class/Core_Lib_Page.php"); //翻页

//访问权限验证
$userRole = $_SESSION['user_role'];
if($userRole == 1 || $userRole == 8){
	//管理员 || 团队成员
}else{
	if($userRole > 0){
		$url = "../index.php?loginError=2";//没权限
	}else{
		$url = "../index.php?loginError=3";//没登录
	}	
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
}

//get post value
$doFlag = $_POST["doFlag"];
$id = $_POST["id"];
$user_id = $_SESSION['user_id'];

//do actions
if($doFlag == "addDiary"){
	//新加 今日工作
	$diaryContent = $_POST["diaryContent"];	
	$sql = "INSERT INTO teamdiary (user_id,diary_content,create_time) VALUES ($user_id,'$diaryContent',NOW())";
	$db->Execute($sql);
}else if($doFlag == "editDiary"){
	//编辑 今日工作
	$id = $_POST["id"];
	$diary_content = $_POST["diary_content_$id"];
    $sql = "update teamdiary set diary_content = '".$diary_content."'";
	$sql = $sql."  where id=".$id;	
	$db->Execute($sql);
}

//今日工作是否已经提交过;
$isFirst = true;
$sql = "select count(*) sumdiary from teamdiary  where user_id=$user_id and date(create_time)=curdate()";
$result = $db->Execute($sql);
if(!$result->EOF) {
	$sumdiary = $result->fields['sumdiary'];
	if($sumdiary>0) $isFirst = false;
}
if($isFirst){
	$todayButton = "<a href='javascript:addDiary()' class='button icon add'>今日工作</a>";
}else{
	$todayButton = "<a href='javascript:addDiary2()' class='button icon add'>今日工作</a>";
}


//统计当年工作时间
$totalDiary = 0;
$sql = "select count(*) sumdiary from teamdiary  where user_id=$user_id and  YEAR(create_time)=YEAR(NOW())";
$result = $db->Execute($sql);
if(!$result->EOF) {
	$sumdiary = $result->fields['sumdiary'];
	if($sumdiary>0) $totalDiary = $sumdiary;
}




//get customers list, 翻页
$listFlag = $_POST["listFlag"];
$list_rows = 20; //每页显示的记录数
$sql_all = "SELECT COUNT(*) as total_rows from teamdiary where user_id=$user_id"; 

$result = $db->Execute($sql_all);
while (!$result->EOF) {
	$totalRows = $result->fields['total_rows'];
	$result->MoveNext();
}
$nowPage = $_POST['nowPage'];
$nowPage = !empty($nowPage) ? $nowPage : 1;
$params = array(
	'total_rows'=>$totalRows,
	'list_rows'=>$list_rows,
	'method'    =>'ajax',
	'ajax_func_name' =>'goToPage',
	'now_page' => $nowPage 
);
$page = new Core_Lib_Page($params);
$limit_m = $page->first_row;
$limit_n = $list_rows;


$sql_list = "select * from teamdiary where user_id=$user_id order by id desc limit $limit_m,$limit_n";//default all

$result = $db->Execute($sql_list);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<!--Begin:head-->
<? $pageTitle = "今日工作内容"; ?>
<?php include("../head.php"); ?>
<!--End:head-->


<body>
<?php include("../topMenu.php"); ?>


<?php include("myteamMenu.php"); ?>


<form name="listForm" action="myDiary.php" method="post">
<div id="container" >

<div id="newDiary">
<div class="leftDiv">
<table boder=0><tr>
<td><textarea name="diaryContent" id="diaryContent" rows=6 cols=60></textarea>
<br>(1.简要记录今天的工作内容; 2.单项工作内容超过4小时的,请注明;)
</td>
<td width=20>&nbsp;</td>
<td><?php echo $todayButton;?></td>
</tr></table>
</div>
</div>
<div class="clearBoth">&nbsp;</div>

<b><font color='blue'>[<?php echo $user_name;?>,本年度已经工作<?php echo $totalDiary;?>天]</font></b><br>

<?php
if($doFlag == "editDiary"){
	echo "<font color='red'>今日工作内容编辑成功!</font><br>";
}
?>

<div class="grayTable">	
<table >
<thead>
	<tr>
	    <th scope="col" >创建日期</th>
		<th scope="col" >工作内容</th>
		<th scope="col" >编辑(今日)</th>
	</tr>
</thead>

<tbody>
	<?php
	$commonFunction = new Common_Function();
	$today_date = date('Y-m-d');
	while (!$result->EOF) {
		$id = $result->fields['id'];
		$create_time = $result->fields['create_time'];
		$diary_content = $result->fields['diary_content'];		
		$createTime_date = substr($create_time,0,10);		
		if($createTime_date==$today_date){
			//当天的记录,可编辑
			$actionStr = "<a href='javascript:doEdit($id)' class='button icon edit'>更新</a>";
			$diary_content = "<textarea  id='diary_content_$id' name='diary_content_$id' rows=5 cols=60>$diary_content</textarea>";
		}else{
			//历史记录,不可编辑
			$actionStr = "存档";
			//$diary_content = "<textarea  id='diary_content_$id' name='diary_content_$id' rows=5 cols=60>$diary_content</textarea>";
			$diary_content = "<div id='diary_content_2_$id'>$diary_content</div>";
		}		
		//-------------------------------------------------
		$strTR = "<tr >";
		$strTR = $strTR ."<td>".$create_time ."</td>";
		$strTR = $strTR ."<td>".$diary_content ."</td>";
		$strTR = $strTR ."<td>".$actionStr."</td>";
		$strTR =  $strTR ."</tr >";
		$strTR =  $strTR ."<script> ";
		$strTR =  $strTR ."$('#diary_content_2_$id').html(replaceEnter2BR($('#diary_content_2_$id').html()));";
		$strTR =  $strTR ."</script> ";
		echo $strTR;
		$result->MoveNext();
	}
	?>
</tbody>

					
</table>

<br>

<!--Page 分页,翻页 -->
<div id="page"><?php echo $page->show(2);?></div>

</div>



</div><!--end container -->

<input type="hidden" name="doFlag" id="doFlag" >
<input type="hidden" name="id" id="id" >
<input type="hidden" name="nowPage" id="nowPage" value="<?php echo $nowPage; ?>"><!--翻页-->
</form>


<form name="inputForm" action="mydiaryInput.php" method="post">
<input type="hidden" name="editId" id="editId" >
</form>



</body>




</html>

<script>
/**翻页**/
function goToPage(nowPage){
	document.getElementById('doFlag').value = 'toList';
	document.getElementById('nowPage').value = nowPage;
	document.listForm.submit();
}

/** 添加 今日工作内容 **/
function addDiary(){
	var diaryContent = $("#diaryContent").val();	
	if(diaryContent==""){
		alert("工作内容不能为空.");
		$("#diaryContent").focus();  
		return;
	}else{
		document.getElementById('doFlag').value = "addDiary";
		document.listForm.submit();
	}
}

/** 多次添加 今日工作内容 **/
function addDiary2(){
	alert("今日工作内容已经存在, 请在下面直接更新.");
}


/** 编辑 今日工作**/
function doEdit(id){
	document.getElementById('doFlag').value = "editDiary";
	document.getElementById('id').value = id;
	document.listForm.submit();
}




</script>

